//
//  ScanTool.swift
//  GreenDeal
//
//  Created by Moennig Q on 16/11/2022.
//

import UIKit
import AVFoundation
enum ScanningType: Int{
    case panel = 1
    case inverter
    case tank//热水器
}
struct SessionQuality {
    var val: AVCaptureSession.Preset;
    var h:CGFloat;
    var w:CGFloat;
}

struct SessionConfig {
    static let quality_low = SessionQuality(val: AVCaptureSession.Preset(rawValue: "AVCaptureSessionPreset640x480"), h: 640, w: 480)
    static let quality_1280x720 = SessionQuality(val: AVCaptureSession.Preset(rawValue: "AVCaptureSessionPreset1280x720"), h: 1280, w: 720)
    static let quality_1920x1080 = SessionQuality(val: AVCaptureSession.Preset(rawValue: "AVCaptureSessionPreset1920x1080"), h: 1920, w: 1080)
    static let quality_3840x2160 = SessionQuality(val: AVCaptureSession.Preset(rawValue: "AVCaptureSessionPreset3840x2160"), h: 3840, w: 2160)
    static var curQuality = quality_low
}
class ScanResultImageTool {
    //四个角和扫描区域内间距
    let margin: CGFloat = 12
    
    static var shared = ScanResultImageTool()
    //MARK: - 获取扫描区域
    func getCropRect(_ scanMode: ScanMode)->CGRect{
        if scanMode == .oneD {
             return CGRect(x: (APPConfig.WidthForPortrait - APPConfig.scanViewWidth) / 2 + margin, y: APPConfig.scanViewTopMargin + margin, width: APPConfig.scanViewWidth - margin * 2, height: APPConfig.scanViewHeight - margin * 2)
        }else{
            return CGRect(x: (APPConfig.WidthForPortrait - APPConfig.qrScanViewWidth) / 2 + margin, y: APPConfig.scanViewTopMargin + margin, width: APPConfig.qrScanViewWidth - margin * 2, height: APPConfig.qrScanViewWidth - margin * 2)
        }
    }
    //MARK: - 获取扫描区域
    func getScanFrameRect(_ scanMode: ScanMode)->CGRect{
        if scanMode == .oneD {
             return CGRect(x: (APPConfig.WidthForPortrait - APPConfig.scanViewWidth) / 2, y: APPConfig.scanViewTopMargin, width: APPConfig.scanViewWidth, height: APPConfig.scanViewHeight)
        }else{
            return CGRect(x: (APPConfig.WidthForPortrait - APPConfig.qrScanViewWidth) / 2, y: APPConfig.scanViewTopMargin, width: APPConfig.qrScanViewWidth, height: APPConfig.qrScanViewWidth)
        }
    }
    //MARK: - 根据原图，指定size重画image
    func downscaleImage(image: UIImage, toSize targetSize: CGSize, scale: CGFloat = 0.0) -> UIImage? {
        let actualScaleFactor = (scale == 0.0) ? UIScreen.main.scale : scale
        let size = image.size
        let imageScaleFactor = image.scale
        let imagePixelSize = CGSizeMake(size.width * imageScaleFactor, size.height * imageScaleFactor)
        
        let requiredMinPixelSize = CGSizeMake(targetSize.width * actualScaleFactor, targetSize.height * actualScaleFactor)
        let canBeDownscaled = (requiredMinPixelSize.width < imagePixelSize.width) && (requiredMinPixelSize.height < imagePixelSize.height)
        
        if !canBeDownscaled {
            return image
        }
        
        let widthRatio  = targetSize.width  / size.width
        let heightRatio = targetSize.height / size.height
        
        // This part is based on https://gist.github.com/hcatlin/180e81cd961573e3c54d
        // but it fixes the bug with wrong ratio used
        let downscaledImageSize: CGSize
        if (widthRatio > heightRatio) {
            downscaledImageSize = CGSizeMake(size.width * widthRatio,  size.height * widthRatio)
        } else {
            downscaledImageSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
        }
        let imageRect: CGRect
        if CGSizeEqualToSize(downscaledImageSize, targetSize) {
            imageRect = CGRectMake(0, 0, targetSize.width, targetSize.height)
        } else {
            let xDiff = (downscaledImageSize.width > targetSize.width) ? 0.5 * (downscaledImageSize.width - targetSize.width) : 0.0
            let yDiff = (downscaledImageSize.height > targetSize.height) ? 0.5 * (downscaledImageSize.height - targetSize.height) : 0.0
            imageRect = CGRectMake(-xDiff, -yDiff, downscaledImageSize.width, downscaledImageSize.height)
        }
        //替换为UIGraphicsImageRenderer，
        let renderer = UIGraphicsImageRenderer(size: targetSize)
        let newImage = renderer.image{ context in
            image.draw(in: imageRect)
        }
        return newImage
    }
}
